package com.bdqn.gulimall.order.controller;

import com.bdqn.gulimall.order.entity.OrderEntity;
import com.bdqn.gulimall.order.entity.OrderReturnApplyEntity;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;

/**
 * RabbitController
 *
 * @author f
 * @since 2022/3/8
 */
@RestController
public class RabbitController {

    @Resource
    RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMq")
    public String sendMq(@RequestParam(value = "num",defaultValue = "10") Integer num) {
        for (int i = 0; i < num; i++) {
            if (i % 2 == 0) {
                OrderReturnApplyEntity returnApplyEntity = new OrderReturnApplyEntity();
                returnApplyEntity.setId(1L);
                returnApplyEntity.setCreateTime(new Date());
                returnApplyEntity.setProductName("好好-->" + i);
                rabbitTemplate.convertAndSend("exchange", "hello.java",
                        returnApplyEntity,new CorrelationData(UUID.randomUUID().toString()));

            } else {
                OrderEntity entity = new OrderEntity();
                entity.setOrderSn(UUID.randomUUID().toString());
                rabbitTemplate.convertAndSend("exchange", "hello.java", entity,
                        new CorrelationData(UUID.randomUUID().toString()));
            }

        }
        return "ok";
    }
}
